using System;
using System.Text ;
using System.Data ;
using System.Collections ;
using System.Windows.Forms;
using WP.MF.BusinessRule; 
using WP.MF.DataEntity; 
using WP.MF.DataAccess; 
using WP.IM.DataAccess; 
using WP.Commons;
using WP.DB;

namespace WP.MF.BusinessLogical
{
	/// <summary>
	/// Summary description for IMinvMasterBLL.
	/// </summary>
    public class MLocationBLL : IBusinessLogical
	{
		MLocationDAL mLocationDAL = new MLocationDAL();
		public MLocationBLL()
		{
			//
			// TODO: Add constructor logic here
			//
		}
        #region Select Record by RECID
        public IDataEntity SelectDataByRecID(string strRecID)
        {
            DBAccess dBAccess = new DBAccess(false, false);
            try
            {
                MLocationDEL mLocationDEL = mLocationDAL.SelectDataByRecID(dBAccess, strRecID);
                return mLocationDEL as IDataEntity;
            }
            catch (Exception e)
            {
                ExceptionManager.ProcessException(e);
                return null;
            }
            finally
            {
                dBAccess.CloseCon();
            }
        }
        #endregion

        #region Get All location main info
        public MLocationDEL SelectMlocationByKey(string strRecId)
        {
            DBAccess dBAccess = new DBAccess(false, false);

            try
            {
                //Get location Info
                MLocationDEL mLocationDEL = new MLocationDEL();
                DataFunctions.SelectByRecID(dBAccess, mLocationDEL, MLocationDEL.TABLENAME_MLOCATION, mLocationDAL.FieldNames, strRecId);

                return mLocationDEL;
            }
            catch (Exception e)
            {
                ExceptionManager.ProcessException(e);
                return null;
            }
            finally
            {
                dBAccess.CloseCon();
            }
        }
        #endregion

		#region Insert into Location
        public bool Insert(IDataEntity imLocationDEL)
		{
            MLocationDEL mLoationDEL = imLocationDEL as MLocationDEL;
			DBAccess dBAccess=null;
			MLocationBRL mLocationBRL=new MLocationBRL();
			
			dBAccess=new DBAccess(false,true);
            try
            {
                ////BaseFunctions.SpecialCharDeal(mLoationDEL);
                if (mLocationBRL.InsertValid(dBAccess, mLoationDEL))
                {
                    mLocationDAL.InsertMLocation(dBAccess, mLoationDEL);
                    dBAccess.CommitTranAndCloseCon();
                    return true;
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                    return false;
                }

            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
		}
		#endregion

		#region Update Location
        public bool Update(IDataEntity imLocationDEL)
		{
            MLocationDEL mLoationDEL = imLocationDEL as MLocationDEL;
            DBAccess dBAccess = null;
            MLocationBRL mLocationBRL = new MLocationBRL();

            dBAccess = new DBAccess(false, true);
            try
            {
                ////BaseFunctions.SpecialCharDeal(mLoationDEL);
                if (mLocationBRL.UpdateValid(dBAccess, mLoationDEL))
                {
                    mLocationDAL.UpdateMLocation(dBAccess, mLoationDEL);
                    dBAccess.CommitTranAndCloseCon();
                    return true;
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                    return false;
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
		}

		#endregion

        #region Delete location
        public bool Delete(string strRecId, byte[] btOldRecStamp)
        {
            DBAccess dBAccess = new DBAccess(false, true);
            MLocationBRL mLocationBRL = new MLocationBRL();

            try
            {
                if (mLocationBRL.DeleteValid(dBAccess, strRecId, btOldRecStamp))
                {
                    if (MessageBox.Show(Msg.Select("Are you sure to delete the location?", DataFunctions.GetMsg(this.GetType().Name, "MSG_DeleteTheLoc")), SysVar.ConfirmTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    {
                        dBAccess.RollbackTranAndClose();
                        return false;
                    }
                    mLocationDAL.DeleteMLocation(dBAccess, strRecId);
                    dBAccess.CommitTranAndCloseCon();
                    return true;
                }
                else
                {
                    dBAccess.RollbackTranAndClose();
                    if (SysVar.ErrorMessage.Count > 0)
                    {
                        MsgBoxEx.Show(SysVar.ErrorMessage, SysVar.WarnTitle);
                    }
                    return false;
                }
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
        }
        #endregion

        #region Insert mUserLocation
        public bool InsertmUserLocation(DataRow dr)
        {
            DBAccess dBAccess = new DBAccess(false, true);

            try
            {
                //Insert Into mUserLocation
                mLocationDAL.InsertmUserLocation(dBAccess, dr);
                dBAccess.CommitTranAndCloseCon();
                return true;
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
        }
        #endregion

        #region Update mUserLocation
        public bool UpdatemUserLocation(DataRow dr)
        {
            DBAccess dBAccess = new DBAccess(false, true);

            try
            {
                //Update mUserLocation 
                mLocationDAL.UpdatemUserLocation(dBAccess, dr);
                dBAccess.CommitTranAndCloseCon();
                return true;
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
        }

        #endregion

       

        public bool InsertmLocationSeller(DataRow drDtl)
        {
            DBAccess dBAccess = new DBAccess(false, true);

            try
            {
                //Insert Into mSellerLocation
                mLocationDAL.InsertmLocationSeller(dBAccess, drDtl);
                dBAccess.CommitTranAndCloseCon();
                return true;
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
        }

        public bool UpdatemLocationSeller(DataRow drDtl)
        {
            DBAccess dBAccess = new DBAccess(false, true);

            try
            {
                //Update mSellerLocation 
                mLocationDAL.UpdatemLocationSeller(dBAccess, drDtl);
                dBAccess.CommitTranAndCloseCon();
                return true;
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
        }

        public bool InsertmLocationBuyer(DataRow drDtl)
        {
            DBAccess dBAccess = new DBAccess(false, true);

            try
            {
                //Insert Into mBuyerLocation
                mLocationDAL.InsertmLocationBuyer(dBAccess, drDtl);
                dBAccess.CommitTranAndCloseCon();
                return true;
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
        }

        public bool UpdatemLocationBuyer(DataRow drDtl)
        {
            DBAccess dBAccess = new DBAccess(false, true);

            try
            {
                //Update mBuyerLocation 
                mLocationDAL.UpdatemLocationBuyer(dBAccess, drDtl);
                dBAccess.CommitTranAndCloseCon();
                return true;
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
        }
         
        public bool DeleteBuyer(string strRecId)
        {
            DBAccess dBAccess = new DBAccess(false, true);


            try
            {
                mLocationDAL.DeleteBuyer(dBAccess, strRecId);

                    dBAccess.CommitTranAndCloseCon();

                    return true;
                
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
        }

        public bool DeleteSeller(string strRecId)
        {
            DBAccess dBAccess = new DBAccess(false, true);


            try
            {
                mLocationDAL.DeleteSeller(dBAccess, strRecId);

                dBAccess.CommitTranAndCloseCon();

                return true;

            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
        }
        public bool InsertTSO(DataRow drDtl)
        {
            DBAccess dBAccess = new DBAccess(false, true);

            try
            {
                //Insert Into mSellerLocation
                mLocationDAL.InsertTSO(dBAccess, drDtl);
                dBAccess.CommitTranAndCloseCon();
                return true;
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
        }

        public bool UpdateTSO(DataRow drDtl)
        {
            DBAccess dBAccess = new DBAccess(false, true);

            try
            {
                //Update mSellerLocation 
                mLocationDAL.UpdateTSO(dBAccess, drDtl);
                dBAccess.CommitTranAndCloseCon();
                return true;
            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
        }
        public bool DeleteTSO(string strRecId)
        {
            DBAccess dBAccess = new DBAccess(false, true);


            try
            {
                mLocationDAL.DeleteTSO(dBAccess, strRecId);

                dBAccess.CommitTranAndCloseCon();

                return true;

            }
            catch (Exception e)
            {
                dBAccess.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
        }

        #region AisleNo
        public bool UpdateAisle(DataRow dr)
        {
            DBAccess dbcon = new DBAccess(false, true);
            try
            {
                dbcon.BeginTran();
                mLocationDAL.UpdateAisle(dbcon, dr);
                dbcon.CommitTranAndCloseCon();
                return true;
            }
            catch (Exception e)
            {
                dbcon.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
            return true;
        }
        public bool InsertAisle(DataRow dr)
        {
            DBAccess dbcon = new DBAccess(false, true);
            try
            {
                dbcon.BeginTran();
                mLocationDAL.InsertAisle(dbcon, dr);
                dbcon.CommitTranAndCloseCon();
                return true;
            }
            catch (Exception e)
            {
                dbcon.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
            return true;
        }
        public bool DeleteAisle(string strLocNo, string strAisleNo)
        {
            DBAccess dbcon = new DBAccess(false, true);
            try
            {
                dbcon.BeginTran();
                mLocationDAL.DeleteAisle(dbcon, strLocNo, strAisleNo);
                dbcon.CommitTranAndCloseCon();
                return true;
            }
            catch (Exception e)
            {
                dbcon.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
            return true;
        }
        #endregion

        #region Racko
        public bool UpdateRack(DataRow dr)
        {
            DBAccess dbcon = new DBAccess(false, true);
            try
            {
                dbcon.BeginTran();
                mLocationDAL.UpdateRack(dbcon, dr);
                dbcon.CommitTranAndCloseCon();
                return true;
            }
            catch (Exception e)
            {
                dbcon.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
            return true;
        }
        public bool InsertRack(DataRow dr)
        {
            DBAccess dbcon = new DBAccess(false, true);
            try
            {
                dbcon.BeginTran();
                mLocationDAL.InsertRack(dbcon, dr);
                dbcon.CommitTranAndCloseCon();
                return true;
            }
            catch (Exception e)
            {
                dbcon.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
            return true;
        }
        public bool DeleteRack(string strLocNo, string strAisleNo, string strRackNo)
        {
            DBAccess dbcon = new DBAccess(false, true);
            try
            {
                dbcon.BeginTran();
                mLocationDAL.DeleteRack(dbcon, strLocNo, strAisleNo, strRackNo);
                dbcon.CommitTranAndCloseCon();
                return true;
            }
            catch (Exception e)
            {
                dbcon.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
            return true;
        }
        #endregion


        #region Bin
        public bool UpdateBin(DataRow dr)
        {
            DBAccess dbcon = new DBAccess(false, true);
            try
            {
                dbcon.BeginTran();
                mLocationDAL.UpdateBin(dbcon, dr);
                dbcon.CommitTranAndCloseCon();
                return true;
            }
            catch (Exception e)
            {
                dbcon.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
            return true;
        }
        public bool InsertBin(DataRow dr)
        {
            DBAccess dbcon = new DBAccess(false, true);
            try
            {
                dbcon.BeginTran();
                mLocationDAL.InsertBin(dbcon, dr);
                dbcon.CommitTranAndCloseCon();
                return true;
            }
            catch (Exception e)
            {
                dbcon.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
            return true;
        }
        public bool DeleteBin(int iRecID)
        {
            DBAccess dbcon = new DBAccess(false, true);
            try
            {
                dbcon.BeginTran();
                mLocationDAL.DeleteBin(dbcon, iRecID);
                dbcon.CommitTranAndCloseCon();
                return true;
            }
            catch (Exception e)
            {
                dbcon.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
            return true;
        }
        #endregion

        public bool GenerateBinLoc(string strLocNo)
        {
            DBAccess dbcon = new DBAccess(false, true);
            try
            {
                dbcon.BeginTran();
                mLocationDAL.GenerateBinLoc(dbcon, strLocNo);
                dbcon.CommitTranAndCloseCon();
                return true;
            }
            catch (Exception e)
            {
                dbcon.RollbackTranAndClose();
                ExceptionManager.ProcessException(e);
                return false;
            }
            return true;
        }
    }
}
